package com.yy.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;


import com.yy.common.annotation.Log;
import com.yy.common.core.controller.BaseController;
import com.yy.common.core.domain.AjaxResult;
import com.yy.common.core.page.TableDataInfo;
import com.yy.common.enums.BusinessType;
import com.yy.common.utils.StringUtils;
import com.yy.common.utils.poi.ExcelUtil;
import com.yy.domain.PayContract;
import com.yy.domain.PayContractFee;
import com.yy.domain.PayFeeItem;
import com.yy.dto.PayContractAddDTO;
import com.yy.dto.PayContractQueryDTO;
import com.yy.service.IPayContractService;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;









/**
 * 出租出售合同Controller
 *
 * @author yy
 * @date 2023-10-30
 */
@RestController
@RequestMapping("/pay/contract")
public class PayContractController extends BaseController
{
    @Autowired
    private IPayContractService payContractService;


    /**
     * 新增出租出售合同
     */
    @PreAuthorize("@ss.hasPermi('pay:contract:add')")
    @Log(title = "出租出售合同", businessType = BusinessType.INSERT)
    @PostMapping("addContract")
    @ApiOperation("新增出租出售合同")
    public AjaxResult addContract(@RequestBody  @Validated PayContractAddDTO payContractAddDTO)
    {
        return toAjax(payContractService.insertPayContract(payContractAddDTO));
    }

    /**
     * 根据商铺ID和其它条件查询合同列表
     */
    @PreAuthorize("@ss.hasPermi('pay:contract:list')")
    @GetMapping("/listContractByHouseId")
    @ApiOperation("根据商铺ID和其它条件查询合同列表")
    public AjaxResult listContractByHouseId(PayContractQueryDTO payContractQueryDTO)
    {
        String houseId = payContractQueryDTO.getHouseId();
        if (StringUtils.isBlank(houseId)) {
            return AjaxResult.error("商铺ID不能为空");
        }
        List<PayContract> list = payContractService.listContractByHouseId(payContractQueryDTO);
        return AjaxResult.success(list);
    }


    /**
     * 根据合同ID查询合同的收费项目
     */
    @PreAuthorize("@ss.hasPermi('pay:contract:list')")
    @GetMapping("/listContractFeeItemByContractId/{id}")
    @ApiOperation("根据商铺ID和其它条件查询合同列表")
    public AjaxResult listContractFeeItemByContractId(@PathVariable Long id)
    {
        List<PayContractFee> list = payContractService.listContractFeeItemByContractId(id);
        return AjaxResult.success(list);
    }
    /**
     * 根据合同ID撤销合同
     */
    @PreAuthorize("@ss.hasPermi('pay:contract:cancel')")
    @GetMapping("/cancelContract/{id}")
    @ApiOperation("根据合同ID撤销合同")
    public AjaxResult cancelContract(@PathVariable Long id)
    {
        int row=this.payContractService.cancelContract(id);
        return toAjax(row>0);
    }





    /**
     * 查询出租出售合同列表
     */
    @PreAuthorize("@ss.hasPermi('pay:contract:list')")
    @GetMapping("/listBillContract")
    public TableDataInfo listBillContract(PayContract payContract)
    {
        startPage();
        List<PayContract> list = payContractService.selectPayContractList(payContract);
        return getDataTable(list);
    }


    /**
     * 导出出租出售合同列表
     */
    @PreAuthorize("@ss.hasPermi('pay:contract:export')")
    @Log(title = "出租出售合同", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PayContract payContract)
    {
        List<PayContract> list = payContractService.selectPayContractList(payContract);
        ExcelUtil<PayContract> util = new ExcelUtil<PayContract>(PayContract.class);
        util.exportExcel(response, list, "出租出售合同数据");
    }

    /**
     * 获取出租出售合同详细信息
     */
    @PreAuthorize("@ss.hasPermi('pay:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(payContractService.selectPayContractById(id));
    }

    /**
     * 根据合同ID查询当前合同待收费的项目
     */
    @PreAuthorize("@ss.hasPermi('pay:contract:list')")
    @GetMapping("getContractFeeItemsByContractId/{contractId}")
    @ApiOperation("根据合同ID查询当前合同待收费的项目")
    public AjaxResult getContractFeeItemsByContractId(@PathVariable Long contractId){
        List<PayFeeItem> payFeeItemList=this.payContractService.getContractFeeItemsByContractId(contractId);
        return AjaxResult.success(payFeeItemList);
    }

}
